

class Resizer {
  container= null;
  camera= null;
  renderer= null;
  frustumSize= null;
  constructor(
    container,
    camera,
    renderer,
  ) {
    this.container = container;
    this.camera = camera;
    this.renderer = renderer;
    this.frustumSize = 16;
    // set initial size
    this.setSize(container, camera, renderer)

    const resizeObserver = new ResizeObserver(entries => {
        //回调
        // set the size again if a resize occurs
        // container.style.width = container.parentNode.style.width;
        // container.style.height = container.parentNode.style.height;
      this.setSize(container, camera, renderer)
      // perform any custom actions
      // this.onResize()
    });

    //监听对应的dom
    resizeObserver.observe(container.parentNode);

    window.addEventListener('resize', () => {
      // set the size again if a resize occurs
      this.setSize(container, camera, renderer)
      // perform any custom actions
      // this.onResize()
    })
  }
  setFrustumSize(val){
    this.frustumSize = val
  }
  setSize(
    container,
    camera,
    renderer,
  ) {
    
   
    renderer.setSize(container.clientWidth, container.clientHeight)
    renderer.setPixelRatio(1.5)

    // this.frustumSize = 1.3;
    const aspect = container.clientWidth / container.clientHeight;

    camera.left = - this.frustumSize * aspect / 2;
    camera.right = this.frustumSize * aspect / 2;
    camera.top = this.frustumSize / 2;
    camera.bottom = - this.frustumSize / 2;

    camera.updateProjectionMatrix();

    renderer.setSize( container.clientWidth, container.clientHeight );
    camera.aspect = container.clientWidth / container.clientHeight
    camera.updateProjectionMatrix()
   
  }
  onResize() {
    this.setSize(this.container, this.camera, this.renderer);
  }
}

export { Resizer }
